<?php
/*
function checkimg
function info
function info2r
function init
function load
function make
function resized
function save
function str

占位
.#.#.#.#.#.#.#.#.#.#*/
//上传专用
class class_easyimage
{
	var $w;
	var $h;
	var $ext;
	
	var $handle;
	var $quality = 80;
	var $widthMax = 200;

//---------------------------------------------------------------------------------------
	function checkwh($int)
	{
		$int = (int)$int;
		
		if($int == 0)
			return false;
		
		return $this->w > $int || $this->h > $int;
	}
//---------------------------------------------------------------------------------------
	function _bind($im)
	{
		$this->handle = $im;
		list($this->w, $this->h, $this->ext) = $this->image_load2r($im);
	}
//---------------------------------------------------------------------------------------
//$mix 有三种,第一是资源,第二是字符串,第三是文件
	function init($mix)
	{
		if(is_resource($mix))
		{
			$this->handle = $mix;
			list($this->w, $this->h, $this->ext) = $this->image_load2r($this->handle);
			return;
		}
		
		if(strlen($mix) > 100)
		{
			$path = tempnam(sys_get_temp_dir(), _only());
			file_put_contents($path, $mix);
			$mix = $path;
		}
		
		$tmp = $this->image_load($mix);
				
		list($this->w, $this->h, $this->ext) = $tmp;
		$im = $this->image_handle($mix, $this->ext);
		$this->_bind($im);
	}
	//---------------------------------------------------------------------------------------
	function image_handle($path, $ext)
	{
	  switch($ext)
	  {
	    case 'gif' :
					$handle = imagecreatefromgif($path);
					break;
	    case 'jpg' :
					$handle = imagecreatefromjpeg($path);
					break;
	    case 'png' :
					$handle = imagecreatefrompng($path);
					break;
	  }
	  
	  if(!$handle)
	  	return false;
	    
	  return $handle;
	}
	//-------------------------------------------------------------------------------
	function image_load($path)
	{
			$inf = getimagesize($path);
			
			if(!$inf)
				return false;
			
			$w = (int)$inf[0];
			$h = (int)$inf[1];
			$int = $inf[2];
			
			if($inf[2] == 1)
			{
				$ext = 'gif';
			}
			else if($inf[2] == 2)
			{
				$ext = 'jpg';
			}
			else if($inf[2] == 3)
			{
				$ext = 'png';
			}
			else
			{
				$ext = '';
			}
			
			return array($w, $h, $ext);
	}
	//---------------------------------------------------------------------------------------
	function image_load2r($handle)
	{
		if($handle)
		{
			$w = (int)imagesx($handle);
			$h = (int)imagesy($handle);
			$ext = 'png';
			return array($w, $h, $ext);
		}
		
		return [];
	}
//---------------------------------------------------------------------------------------
	function resize($w, $h)
	{
		if($this->w <= $w || $this->h <= $h)
			return;
		
		$im = imagecreatetruecolor($w, $h);
		$ff = imagecolorallocate($im, 255, 255, 255);
		imagecopyresampled($im, $this->handle, 0, 0, 0, 0, $w, $h, $this->w, $this->h);
		$this->_bind($im);
	}
//---------------------------------------------------------------------------------------
//以最长的边为基准,短边补白,图片完整显示
	function resized($w, $h)
	{
		if($this->w > $this->h)
		{
			$nw = $w;
			$nh = $w / $this->w * $this->h;
			
			$x = 0;
			$y = $h > $nh ? (($h - $nh) / 2) : 0;
		}
		else
		{
			$nw = $h / $this->h * $this->w;
			$nh = $h;
			
			$x = $w > $nw ? (($w - $nw) / 2) : 0;
			$y = 0;
		}
		
		$src = $this->handle;
		
		$im = imagecreatetruecolor($nw, $nh);
		$ff = imagecolorallocate($im, 255, 255, 255);
		imagecopyresampled($im, $src, 0, 0, 0, 0, $nw, $nh, $this->w, $this->h);
				
		$im2 = imagecreatetruecolor($w, $h);
		$ff = imagecolorallocate($im2, 255, 255, 255);
		imagefilledrectangle($im2, 0, 0, $w, $h, $ff);
		imagecopymerge($im2, $im, $x, $y, 0, 0, $nw, $nh, 100);
		
		$this->_bind($im2);
	}
//---------------------------------------------------------------------------------------
//以短边为基准,长边截去,图片部分显示,非补白
//一律按宽放大或缩小,如果高无留白,取高中间,如果高有留白,再按高放大,宽取中间
	function resizeCut($w, $h)
	{
		//主要计算出四个变量 $nw $nh $x $y
		
		//一律按宽放大或缩小
		$nw = $w;
		$nh = $w / $this->w * $this->h;
		
		//新高大于最终高,则取中间
		if($nh > $h)
		{
			$x = 0;
			$y = ($nh - $h) / 2;
		}
		else	//新高小于最终高,则下半有白,这时再按最终高放大
		{
			$oh = $nh;
			$ow = $nw;
			
			$nw = $h / $oh * $ow;
			$nh = $h;
			
			//放大后宽肯定大于新宽,则宽取中间
			$y = 0;
			$x = ($nw - $ow) / 2;
		}
		
		$this->zoom($nw, $nh);
		$this->cut($x, $y, $w, $h);
	}
//---------------------------------------------------------------------------------------
//放大或缩小
	function zoom($w, $h)
	{
		$src = $this->handle;
		
		$im = imagecreatetruecolor($w, $h);
		$ff = imagecolorallocate($im, 255, 255, 255);
		imagecopyresampled($im, $src, 0, 0, 0, 0, $w, $h, $this->w, $this->h);
		
		$this->_bind($im);
	}
//---------------------------------------------------------------------------------------
//栽剪
	function cut($x, $y, $w = 0, $h = 0)
	{
		$w = $w ? $w : $this->w;
		$h = $h ? $h : $this->h;
		
		$im = imagecreatetruecolor($w, $h);
		$ff = imagecolorallocate($im, 255, 255, 255);
		imagefilledrectangle($im, 0, 0, $w, $h, $ff);
		imagecopymerge($im, $this->handle, 0, 0, $x, $y, $w, $h, 100);
		
		$this->_bind($im);
	}
//---------------------------------------------------------------------------------------

	function str()
	{
		$im = $this->handle;
		ob_start();
		imageinterlace($im, 1);
		imagejpeg($im, null, $this->quality);
		$str = ob_get_contents();		
		ob_end_clean();
		return $str;
	}
//---------------------------------------------------------------------------------------
	function p()
	{
		$im = $this->handle;
		header("content-type:image/png");
		imagejpeg($im, null, $this->quality);
	}
//---------------------------------------------------------------------------------------
	//生成水印图片
	protected function mark($str, $str2)
	{
		$im = $this->handle;
		$w = imagesx($im);
		$h = imagesy($im);
		$intp = 0.05;	//水印十分之一大
		$w2 = round($w * $intp);
		$w2 = floor($w2 / 10) * 10;
		$h2 = round($h * $intp);
		$h2 = floor($h2 / 10) * 10;
		
		$id = md5("{$str}-{$str2}-{$w2}-{$h2}");
		$path = $GLOBALS['DIR_CACHE'] . '/' . $id . '.png';
		
		if(!_fh($path))
		{
			$fontfile = $GLOBALS['DIR_FONTS'].'/yahei.ttf';
			$array = range(10, 200, 5);
			$size = 0;
			
			foreach($array as $v)
			{
				$params = imagettfbbox($v, 0, $fontfile, $str);
				$th = $params[1] - $params[7];
				$tw = $params[2] - $params[0];
				if($th > $h2)
				{
					$size = $v;
					break;
				}
			}
			
			$size2 = round($size/2);
			$params = imagettfbbox($size2, 0, $fontfile, $str2);
			$tw2 = $params[2] - $params[0];	//下层字长度
			
			$bw = round($w / 100);	//水印空边
			$bh = round($h / 50);		//水印空边
			$tw += $bw;		//边框留空
			$th += $bh;		//边框留空
			$ch = round($th / 2);	//下层字空间
			$th += $ch;	//总空间
			
			if($tw2 > $tw)
				$tw = $tw2;
			
			$im2 = imagecreatetruecolor($tw, $th);
			$ff = imagecolorallocatealpha($im2, 255, 255, 255, 0);
			$bb = imagecolorallocatealpha($im2, 0, 0, 0, 0);
			imagefilledrectangle($im2, 0, 0, $tw, $th, $ff);
			imagecolortransparent($im2, $ff);
			imagettftext($im2, $size, 0, 0, $th-$ch-$bh, $bb, $fontfile, $str);
			imagettftext($im2, $size2, 0, round(($tw - $tw2)/2), $th-round($ch/2), $bb, $fontfile, $str2);
			
			//如果图超出最大宽度
			if($tw > ($this->widthMax + 50))
			{
				$sx = $this->widthMax;
				$sy = $this->widthMax / $tw * $th;
				$small = imagecreatetruecolor($sx,$sy);
				$fff = imagecolorallocatealpha($small, 255, 255, 255, 0);
				imagefilledrectangle($small, 0, 0, $sx, $sy, $fff);
				imagecolortransparent($small, $fff);
				imagecopyresized($small,$im2,0,0,0,0,$sx,$sy,$tw,$th);
				imagedestroy($im2);
				$im2 = $small;
			}
			
			imagepng($im2, $path);
			imagedestroy($im2);
		}
		
		return $path;
	}
//---------------------------------------------------------------------------------------
	function water($str, $str2 = '', $alpha = 50, $num = 1)
	{
		$path = $this->mark($str, $str2);
		$this->imgmark($path, $alpha, $num);
	}
//---------------------------------------------------------------------------------------
	function imgmark($mix, $alpha = 50, $num = 1)
	{
		if(is_resource($mix))
		{
			$im2 = $mix;
		}
		else if(strlen($mix) < 100)
		{
			$mix = file_get_contents($mix);
			$im2 = imagecreatefromstring($mix);
		}
		else
		{
			$im2 = imagecreatefromstring($mix);
		}
    
    $w2 = imagesx($im2);
    $h2 = imagesy($im2);

		$im = $this->handle;
    $w = imagesx($im);
    $h = imagesy($im);
		
		for($i=0;$i<$num;$i++)
		{
			$dx = rand(10, $w - $w2 - 10);
			$dy = rand(0, $h - $h2);
			imagecopymerge($im, $im2, $dx, $dy, 0, 0, $w2, $h2, $alpha);
		}
	}
//-------------------------------------------------------------------------------
	function image_checkupload(&$err, $name, $defaultext = 'pic', $maxsize = '2m', $wh = '')
	{
			$stat = $_FILES[$name]['error'] === UPLOAD_ERR_OK;
			$path = $_FILES[$name]['tmp_name'];
			$name = $_FILES[$name]['name'];
			$size = $_FILES[$name]['size'];
	
			if(!$stat || $path == '')
			{
				$err = '上传错误';
				return false;
			}
			
			$inf = $this->image_load($path);
			if(!$inf)
			{
				$err = '上传图片格式错误';
				return false;
			}
			
			list($w1, $h1, $ext) = $inf;
			
			if($ext == '')
			{
				$err = '无后缀名';
				return false;
			}
	
			if($maxsize == '')
				$maxsize = '2m';
			$maxsize = string_size($maxsize);
			
			if($size > $maxsize)
			{
				$err = '文件大小超出限制';
				return false;
			}
			
			if($defaultext == '' || $defaultext == 'pic')
				$defaultext = ',gif,png,jpg,';
			
			if(strpos($defaultext, ",{$ext},") === false)
			{
				$err = '文件类型不充许';
				return false;
			}
			
			if(!empty($wh))
			{
				list($w, $h, $range) = $wh;
							
				if($w > 0 && abs($w1 - $w) > $range)
				{
					$err = '文件宽度不符合要求';
					return false;
				}
				
				if($h > 0 && abs($h1 - $h) > $range)
				{
					$err = '文件高度不符合要求';
					return false;
				}
			}
			
			return $inf;
	}
}
